/* i_bal_post_attrit.do ********************************************************

Creates estimates for Table A5 (Covariate balance in post-attrition outcome samples)

*******************************************************************************/

/*
Note: Balance on Table 1 + FRPL, LEP, SPED in our post-attrition outcomes
sample sizes:
	- 3,883 in Table 4 (grade progression)
	- 2,613 in Table 5 (SAT)
	- 3,569 in column (2) of Table 6 (MCAS)
*/

* Import dataset
use "$stata_data_analysis/final_sample.dta", clear

local controls hispanic female black white pov dob sp_biling pop ///
	is_biling age_PreK_enr med_inc_hh med_inc_fam med_gross_rent share_black ///
	share_white share_hispanic frpl num_ranked first_walk East any_spd1 any_spd3 ///
	any_spd1 any_spd3 ell_pgm1 ell_pgm3

local instrument 	soffany
local endog			att
local controls		`controls'
local other_controls "i.yr_asgn"
local psc			"pformany"
local restriction	"in_K1"
local suffix		"an_K1"

* Balance with saturated pscore

* Sample: 3,883 in Table 4 (grade progression)
preserve
	local restriction "in_K1 & in_sims"
	get_estimates, endogvar(`endog') instruments(`instrument') idvar(studentno) ///
		balance_vars(`controls') restriction(`restriction') pscore(`psc') other_controls(`other_controls') ///
		outfile("$results/tables/bal_cntrl`suffix'_sims_subsample") balance(1) non_offered_mean(1)
restore

* Sample: 2,613 in Table 5 (SAT)
preserve
	local restriction "in_K1 & in_sims & !mi(has_sat_took_10)"
	get_estimates, endogvar(`endog') instruments(`instrument') idvar(studentno) ///
		balance_vars(`controls') restriction(`restriction') pscore(`psc') other_controls(`other_controls') ///
		outfile("$results/tables/bal_cntrl`suffix'_sat_subsample") balance(1) non_offered_mean(1)
restore

* Sample: 3,569 in column (2) of Table 6 (MCAS)
	use "$stata_data_analysis/final_sample.dta", clear

preserve


	order *, sequential

	keep studentno att in_sims in_K1 soffany yr_asgn hispanic asian female black white ///
		std_scaled_e3-std_scaled_e8 ///
		std_scaled_e10 std_scaled_m3-std_scaled_m8 std_scaled_m10 ELC soff* pform* ///
		frpl not_frpl nLC sp_biling inc1 inc2 inc3 inc4  ///
		std_e3-std_e8 std_e10 std_m3-std_m8 std_m10

	reshape long std_scaled_e std_scaled_m std_e std_m, i(studentno) j(grade)

	keep if std_m != . // to be consistent with column 2 of Table 6
	keep studentno
	duplicates drop

	tempfile student_list
	save `student_list'

restore

merge 1:1 studentno using `student_list', keep(3) nogen

local restriction "in_K1 & in_sims"
get_estimates, endogvar(`endog') instruments(`instrument') idvar(studentno) ///
	balance_vars(`controls') restriction(`restriction') pscore(`psc') other_controls(`other_controls') ///
	outfile("$results/tables/bal_cntrl`suffix'_mcas_subsample") balance(1) non_offered_mean(1)
